package org.zfm.org.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.zfm.org.service.IFastFdfsService;
import org.zfm.util.data.AjaxResult;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private IFastFdfsService fastFdfsService;

    /**
     * 图片上传
     * @param file
     * @return
     * //注意事项 MultipartFile file 参数名，只能是file 否则无法接收到文件 会报空指针
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        try {
            String path = fastFdfsService.upload(file);
            return AjaxResult.getBean().setResult(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 图片删除
     * @param path
     * @return
     */
    @DeleteMapping("/delete")
    public AjaxResult delete(String path) {
        try {
            fastFdfsService.delete(path);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
